<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 20px">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>品牌列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>
    <el-table
        :data="tableData"
        border
        style="width: 100%">
      <el-table-column prop="id" label="ID" ></el-table-column>
      <el-table-column prop="name" label="姓名" ></el-table-column>
      <el-table-column prop="pinyin" label="拼音"></el-table-column>
      <el-table-column prop="logo" label="LOGO"></el-table-column>
      <el-table-column prop="description"  label="详情"></el-table-column>
      <el-table-column prop="keywords"  label="关键词"></el-table-column>
      <el-table-column prop="sort"  label="排列序号"></el-table-column>
      <el-table-column prop="sales"  label="销量"></el-table-column>
      <el-table-column prop="productCount"  label="商品种类数量总和"></el-table-column>
      <el-table-column prop="commentCount"  label="买家评论数量总和"></el-table-column>
      <el-table-column prop="positiveCommentCount"  label="买家好评数量总和"></el-table-column>
      <el-table-column  label="是否启用" align="center" width="80">
        <template slot-scope="scope">
          <el-switch
              @change="changeEnable(scope.row)"
              v-model="scope.row.enable"
              :disabled="scope.row.id==1"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#ff4949">
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column   label="操作">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="handleEdit(scope.row)"  circle></el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="openDeleteConfirm(scope.row)" circle></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--弹出的编辑相册对话框-->
    <el-dialog title="修改相册" :visible.sync="dialogFormVisible">
      <el-form :model="ruleForm">
        <el-form-item label="名称"  :label-width="formLabelWidth">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="品信"  :label-width="formLabelWidth">
          <el-input v-model="ruleForm.pinyin"></el-input>
        </el-form-item>
        <el-form-item label="简介"  :label-width="formLabelWidth">
          <el-input v-model="ruleForm.description"></el-input>
        </el-form-item>
        <el-form-item label="关键词列表"  :label-width="formLabelWidth">
          <el-input v-model="ruleForm.keywords"></el-input>
        </el-form-item>
        <el-form-item label="LOGO"  :label-width="formLabelWidth">
          <el-input v-model="ruleForm.logo"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEdit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      tableData: [],
      ruleForm:{},
      formLabelWidth: '120px',
      dialogFormVisible:false,
    }
  },
  methods:{
    handleEdit(brand){

      let url = 'http://localhost:9080/brands/'+brand.id+'/select';
      console.log('url = '+url);
      this.axios
          .create({'headers':{'Authorization':localStorage.getItem('jwt')}})
          .get(url).then((response)=>{
        let responseBody = response.data;
        if (responseBody.state == 10000){
          this.ruleForm= responseBody.data;
          this.dialogFormVisible= true;
        }else {
          this.$message.error(responseBody.message);
          this.loadBrandList();
        }
      })
      this.$alert(message,'提示',{
        confirmButtonText:'确定'
      });
    },
    submitEdit(){
      let url = 'http://localhost:9080/brands/'+this.ruleForm.id+'/update';
      console.log('url = '+url);
      let formData = this.qs.stringify(this.ruleForm)
      this.axios
          .create({'headers':{'Authorization':localStorage.getItem('jwt')}})
          .post(url,formData).then((response)=>{
        let responseBody = response.data;
        if (responseBody.state == 10000){
          this.$message({
            message:'修改品牌成功',
            type:'success'
          });
          this.dialogFormVisible=false;
          this.loadAlbumList();
        }else if (responseBody.state ==20000){
          this.$message.error(responseBody.message);
        }else {
          this.$message.error(responseBody.message);
          this.dialogFormVisible=false;
          this.loadAlbumList();
        }
      })
    },
    changeEnable(brand){
      let  enableText= ['禁用','启用'];
      let url = 'http://localhost:9080/brands/'+brand.id;
      if (brand.enable==1){
        console.log("将启用品牌")
        url+='/enable'
      }else {
        console.log("将禁用品牌")
        url+='/disable'
      }
      this.axios
          .create({'headers':{'Authorization':localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 10000) {
          let message = '将管品牌【' +brand.name + '】的启用状态改为【' + enableText[brand.enable] + '】成功！';
          this.$message({
            message: message,
            type: 'success'
          });
        } else {
          this.$message.error(responseBody.message);
        }
        this.loadAdminList();
      });
    },
    handleDelete(brand){
      let url = 'http://localhost:9080/brands/'+brand.id+'/delete';
      this.axios
          .create({'headers':{'Authorization':localStorage.getItem('jwt')}})
          .post(url).then((response)=>{
        let responseBody = response.data;
        if (responseBody.state!=10000){
          this.$message.error(responseBody.message);
        }
        this.loadBrandList();
      })
    },
    openDeleteConfirm(brand){
      let message='此操作将永久删除该品牌, 是否继续?';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(brand);
      }).catch(() => {
      });
    },
    loadBrandList(){
      let url = 'http://localhost:9080/brands';
      console.log('url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 10000) {
          this.tableData = responseBody.data;
        } else {
          this.$message.error(responseBody.message);
        }
      });
    }
  },
  mounted() {
    console.log('mounted')
    this.loadBrandList();
  }
}
</script>

